package com.meilai.project.mapper.business.media;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.meilai.project.controller.mini.dto.MiniSourceMTDTO;
import com.meilai.project.entity.media.MediaType;
import com.meilai.project.vo.app.CompanyOfflineMediaTypeDataVO;
import com.meilai.project.vo.business.media.MediaTypeExtVO;
import com.meilai.project.vo.business.media.MediaTypeForBuildingTypeVO;
import com.meilai.project.vo.business.media.MediaTypeTreeVO;
import com.meilai.project.vo.business.media.MediaTypeVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface MediaTypeMapper extends BaseMapper<MediaType> {

    @Select("SELECT " +
            " `mt`.`id` AS `id`, " +
            " `mt`.`pid` AS `pid`, " +
            " `mt`.`rid` AS `rid`, " +
            " IF(mt.pid=4, 2, 1) AS `level`, " +
            " concat( IF ( isnull( `mtp`.`name` ), '', concat( `mtp`.`name`, '-' ) ), `mt`.`name` ) AS `name`, " +
            " '' AS `code`, " +
            " IF(mt.id=9, 'AB面', '') AS `memo`, " +
            " IF(mt.id=9, 'A面,B面', '') AS params " +
            "FROM " +
            "  `media_type` `mt` LEFT JOIN `media_type` `mtp` ON  `mt`.`pid` = `mtp`.`id`  " +
            "WHERE " +
            "  `mt`.`is_leaf` = 1 ")
    List<CompanyOfflineMediaTypeDataVO> getMediaTypesForApp();

    @Select("SELECT id,`name` FROM v_media_type_leaf ORDER BY rid,pid,id;")
    List<MediaTypeVO> getMediaTypes();

    @Select("SELECT a.id,a.`name`,b.type_id AS building_type_id,x.`name` AS building_type,b.type_id_second AS building_type_id_second,y.`name` AS building_type_second " +
            "FROM v_media_type_leaf a " +
            "LEFT JOIN (SELECT DISTINCT bap.media_type_id,ba.type_id,ba.type_id_second " +
            "FROM building_area_point bap  " +
            "JOIN building_area ba ON bap.building_area_id=ba.id) b ON a.id=b.media_type_id " +
            "LEFT JOIN dictionary x ON b.type_id=x.id " +
            "LEFT JOIN dictionary y ON b.type_id_second=y.id " +
            "ORDER BY a.id,b.type_id,b.type_id_second")
    List<MediaTypeForBuildingTypeVO> getMediaTypesWithBuildingType();

    @Select("SELECT id,`name` FROM v_media_type_leaf where rid != 4")
    List<MediaTypeVO> getMediaTypesNotIncludeGasStation();

    @Select("SELECT id,`name` FROM v_media_type_leaf where rid = 4")
    List<MediaTypeVO> getMediaTypesOfGasStation();

    @Select("SELECT id,`name`, IF(rid = 4,1,0) as gas FROM v_media_type_leaf")
    List<MediaTypeExtVO> listMediaTypes();

    List<MediaType> listByNameList(@Param("nameList") List<String> nameList);

    @Select("select `name` from v_media_type_include_proxy")
    List<String> queryAllIncludeProxy();

    List<MediaTypeTreeVO> page(@Param("name") String name);

    List<MediaType> getTypeList(@Param("media_type_id") Long media_type_id);

    List<MiniSourceMTDTO>getAllTypeForMini();
}
